package com.ysq.excavator.action;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.ansj.domain.Term;
import org.ansj.splitWord.analysis.ToAnalysis;
import org.ansj.util.FilterModifWord;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import com.ysq.excavator.config.ConfigInfo;
import com.ysq.excavator.entity.Item;
import com.ysq.excavator.entity.TreeNode;
import com.ysq.excavator.service.HtmlContentService;
import com.ysq.excavator.util.CommonUtil;
import com.ysq.excavator.util.LoggerUtil;

@Controller("helloTestAction")
@Scope("prototype")
public class HelloTestAction extends ActionSupport{
	private static final long serialVersionUID = 1L;
	
	@Resource(name = "htmlContentServiceImpl")
	private HtmlContentService htmlContentService;
	
	private int page;
	private int rows;
	
	private String sort;
	private String order;

	private String id;//树节点id
	
	private List<Long> contentidList = new ArrayList<Long>();
	
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public List<Long> getContentidList() {
		return contentidList;
	}
	public void setContentidList(List<Long> contentidList) {
		this.contentidList = contentidList;
	}
	public void helloWorld(){
		LoggerUtil.logInfo("1111111111");
		
		LoggerUtil.logDebug("debug...");
		
		LoggerUtil.logError("错误信息测试");
		
		htmlContentService.test();
	}
	
	/**
	 * 表格测试
	 */
	public void getItems(){
		List<Item> itemList = new ArrayList<Item>();
		
		//int start = (page-1)*rows;
		
		for(int i=0;i<5;i++){
			Item item = new Item();
			item.setItemid("ItemId" + i);
			item.setProductid("productid" + i);
			item.setListprice(i);
			item.setUnitcost(i);
			item.setAttr1("attr1" + i);
			item.setStatus("status" + i);
			
			itemList.add(item);
		}
		
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("total",100);
		result.put("rows",itemList);
		
		String jsonResult = JSON.toJSONString(result);
		
		CommonUtil.printResult(jsonResult);
	}
	
	/**
	 * 表格测试
	 */
	public void getMaps(){
		List<Map<String, Object>> itemList = new ArrayList<Map<String,Object>>();
		
		for(int i=0;i<10;i++){
			Map<String, Object> mapItem = new HashMap<String, Object>();
			
			mapItem.put("itemid", "ItemId" + i);
			mapItem.put("productid","productid" + i);
			mapItem.put("listprice",i);
			mapItem.put("unitcost",i);
			mapItem.put("attr1","attr1" + i);
			mapItem.put("status","status" + i);
			
			itemList.add(mapItem);
		}
		
		Map<String, Object> mapItem = new HashMap<String, Object>();
		
		mapItem.put("itemid", "ItemId" + 99);
		mapItem.put("productid","productid" + 99);
		mapItem.put("listprice",99);
		mapItem.put("unitcost",99);
		mapItem.put("attr1","attr1" + 99);
		
		itemList.add(mapItem);
		
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("total",100);
		result.put("rows",itemList);
		
		String jsonResult = JSON.toJSONString(result);
		
		CommonUtil.printResult(jsonResult);
	}
	
	/**
	 * 获得树
	 */
	public void loadTree(){
		System.out.println("loadTree...");
		
		List<TreeNode> nodeList = new ArrayList<TreeNode>();

		File indexFolder = new File(ConfigInfo.LUCENE_INDEX_PATH);
		File[] childFiles = indexFolder.listFiles();
		
		for(File file : childFiles){
			String path = file.getPath();
			System.out.println(path);
			TreeNode child = new TreeNode();
			
			child.setId(path);
			child.setText(path);
			child.setState(false);
			nodeList.add(child);
		}
		String jsonResult = JSON.toJSONString(nodeList);
		CommonUtil.printResult(jsonResult);
	}
	
	/**
	 * 删除测试
	 */
	public void initAnsjFilter(){
		System.out.println("deleteTest...");
		
		HashMap<String, String> updateDic = new HashMap<String, String>();
		
		updateDic.put("但是", FilterModifWord._stop);
		updateDic.put("。", FilterModifWord._stop);
		updateDic.put("，", FilterModifWord._stop);
		updateDic.put("！", FilterModifWord._stop);
		
		updateDic.put("你好", FilterModifWord._stop);
		updateDic.put("测试", FilterModifWord._stop);
		
		FilterModifWord.setUpdateDic(updateDic);
		
	}
	
	/**
	 * 分词测试
	 * @param args
	 */
	public void ajsjSplitTest(){
		String text = "你好，是，这里是 ansj 分词过滤测试。但是，不能包含标点符号！";
		
		List<Term> paser = ToAnalysis.parse(text);
		System.out.println(paser);
		
		paser = FilterModifWord.modifResult(paser);
		
		System.out.println(paser);
		
	}
	
	
	public static void main(String[] args) {
		HelloTestAction ha = new HelloTestAction();
		ha.getItems();
	}
	
}
